<template>
	<view class="d">
		<uni-file-picker ref="upfEle"
			v-model="flieLists" 
			fileMediatype="image"
			:autoUpload="false"
			mode="list"
			limit="5"  
			@select="select" 
			@progress="progress" 
			@success="success" 
			@fail="fail" 
		/>
	</view>
</template>

<script>
	import {defineComponent,reactive,toRefs,onMounted,getCurrentInstance,computed} from 'vue'
	import {onLoad,onShow} from '@dcloudio/uni-app'
	import { useStore } from 'vuex'
	import {myUploadFile} from "@/api/base.js"

	export default defineComponent({
		components:{
			
		},
		props:{
		
		},
		setup(props,context) {
			const store = useStore()
			const {proxy} = getCurrentInstance()
			
			const state = reactive({
				userInfo:computed(()=>store.state.base.userInfo),
				flieLists:[]
			})
			
			onMounted(()=>{
				
			})
			
			// 获取上传状态
			const select= async e=>{
				console.log('选择文件：',e)
				// const res = await myUploadFile(e.tempFilePaths,e.tempFiles[0].name)
				// if(res.code == 200){
				// 	proxy.$emit('success',res.data)
				// }else{
				// 	proxy.$emit('fail',res.data)
				// }	
			}
			// 获取上传进度
			const progress=e=>{
				console.log('上传进度：',e)
			}
			
			// 上传成功
			const success=e=>{
				console.log('上传成功')
			}
			
			// 上传失败
			const fail=e=>{
				console.log('上传失败：',e)
			}
			
			const doUploadFile = async ()=>{
				uni.showLoading({
				  	title: '文件上传中...'
				})
				await proxy.$refs.upfEle.upload()  // 调用此方法，获取选择的文件列表，即flieLists值
				console.log("flieLists:",state.flieLists)
				if(state.flieLists.length == 0){
					proxy.$emit('success','')
					return
				}
				const scLists = []
				if(state.flieLists.length>0){
					for(let i=0;i<state.flieLists.length;i++){
						const item = state.flieLists[i]
						const res = await myUploadFile(item.path,item.name)
						if(res.code == 200){
							scLists.push(res.data)
						}else{
							console.log(`${item.name}上传服务器失败`)
						}
					}
				}
				uni.hideLoading();
				proxy.$emit('success',scLists)
			}
			
			return {
				...toRefs(state),
				select,
				progress,
				success,
				fail,
				doUploadFile
			}
		}
	})
</script>

<style lang="scss" scoped>
	
</style>
